<template>
  <div class="basic-layout">
    <div :class="['nav-side', isFold ? 'fold' : 'unfold']">
      <div class="logo">
        <img src="./../assets/logo.png" alt="" />
        <span>Manager</span>
      </div>
      <el-menu
        default-active="1"
        class="nav-menu"
        background-color="#001529"
        text-color="#fff"
        router
      >
        <!-- 渲染请求回来的菜单栏
        回来的菜单分为一级二级(递归调用这个组件渲染二级菜单) -->
        <tree-menu :userMenu="userMenu" />
      </el-menu>
    </div>
    <div :class="['content-right', isFold ? 'fold' : 'unfold']">
      <div class="nav-top">
        <div class="nav-left">
          <div class="menu-fold" @click="toggle">
            <el-icon><Fold /></el-icon>
          </div>
          <div class="bread">
            <bread-crumb />
          </div>
        </div>
        <div class="drop">
          <el-dropdown @command="handleLogout">
            <span class="el-dropdown-link">
              {{ userInfo.userName }}
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item ommand="email"
                  >邮箱:{{ userInfo.userEmail }}</el-dropdown-item
                >
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <div class="wrapper">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import TreeMenu from './TreeMenu.vue'
import BreadCrumb from './BreadCrumb.vue'
import { ref } from 'vue'
import api from './../api'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'

let store = useStore()
let router = useRouter()
let userMenu = ref([])
let isFold = ref(false)
const getPermissionList = async () => {
  let { actionList, menuList } = await api.getPermissionList()
  userMenu.value = menuList
}
getPermissionList()

const toggle = () => {
  isFold.value = !isFold.value
}

let userInfo = store.state.userInfo
const handleLogout = (key) => {
  console.log(key);
  if (key === 'email') return
  // 如果点击是退出
  // 1.清除缓存 2.回到登录页
  store.commit('saveUserInfo', '')
  userInfo = {}
  router.push("/login")
}
</script>

<style lang="scss">
.basic-layout {
  position: relative;
  .nav-side {
    position: fixed;
    width: 200px;
    height: 100vh;
    background-color: #001529;
    color: #fff;
    overflow: hidden;
    transition: width 0.5s;
    .logo {
      display: flex;
      align-items: center;
      font-size: 18px;
      height: 50px;
      img {
        margin: 0 16px;
        width: 32px;
        height: 32px;
      }
    }
    .nav-menu {
      height: calc(100vh - 50px);
      border-right: none;
    }
    // 合并
    &.fold {
      width: 64px;
      .el-sub-menu__title span {
        transition: opacity 0.4s;
        opacity: 0 !important;
      }
      .el-sub-menu__title .el-icon {
        transition: font-size 0.6s ease;
        font-size: 40px;
      }
      .el-sub-menu__icon-arrow {
        transition: opacity 0.4s;
        opacity: 0 !important;
      }
    }
    // 展开
    &.unfold {
      width: 200px;
    }
  }
  .content-right {
    // overflow: hidden;
    margin-left: 200px;
    // 合并
    &.fold {
      margin-left: 64px;
    }
    // 展开
    &.unfold {
      margin-left: 200px;
    }
    .nav-top {
      height: 50px;
      line-height: 50px;
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #ddd;
      padding: 0 20px;
      .nav-left {
        display: flex;
        align-items: center;
        .menu-fold {
          margin-right: 15px;
          font-size: 18px;
        }
      }
      .user-info {
        .notice {
          line-height: 30px;
          margin-right: 15px;
          cursor: pointer;
        }
        .user-link {
          cursor: pointer;
          color: #409eff;
        }
      }
      .drop {
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
    .wrapper {
      background: #eef0f3;
      padding: 20px;
      height: calc(100vh - 50px);
      .main-page {
        background: #fff;
        height: 100%;
      }
    }
  }
}
</style>
